﻿
namespace ShoppingCart.WebSite
{
    using System;
    using System.Configuration;
    using System.IO;
    using System.Web.Security;
    using Business.Logic;
    using WebSite.Code;

    public partial class Recuperar : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void BtnEnviarClick(object sender, EventArgs e)
        {
            try
            {
                var user = Membership.GetUser(txtUsername.Text);
                var cliente = ClienteManager.Instancia.ObtenerPorDni(txtUsername.Text);
                var path = Request.MapPath("~/Templates/RecoveryPassword.htm");
                var reader = new StreamReader(path);
                var template = reader.ReadToEnd();
                template = template.Replace("#MES#", DateTime.Now.ToString("MMMM"));
                template = template.Replace("#ANIO#", DateTime.Now.Year.ToString());
                template = template.Replace("#NOMBRE#", cliente != null ? cliente.ToString() : txtUsername.Text);
                template = template.Replace("#DNI#", txtUsername.Text);
                template = template.Replace("#CONTRASENIA#", user.GetPassword());
                template = template.Replace("#EMAIL#", ConfigurationManager.AppSettings["Email"]);
                SendEmail(user.Email, ConfigurationManager.AppSettings["WebSite"] + " - Contraseña Recuperada", template);
                ClientScript.RegisterStartupScript(GetType(), "mostrar1", "alert('La Contraseña fue enviada a su Correo.'); window.location = 'Default.aspx';", true);
                reader.Dispose();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "mostrar2", "alert('El Número de DNI Ingresado no existe, asegúrese de ingresar correctamente.');", true);
            }
        }
    }
}
